package ar.com.pablomarchesi;

import android.app.Activity;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ToggleButton;

public class AcelerometroActivity extends Activity implements SensorEventListener {

	private float y, z;
	private TextView txtview;
	private SensorManager mSensorManager;
	private float posAcel;
	private boolean isRunning = false;
	private float[] mGravity;
	private float[] mGeomagnetic;

	protected void onResume() {
		super.onResume();

		SensorManager mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
		Sensor accelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
		Sensor magnetometer = mSensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);
		if (accelerometer != null && magnetometer != null) {
			mSensorManager.registerListener(this, accelerometer, SensorManager.SENSOR_DELAY_GAME);
			mSensorManager.registerListener(this, magnetometer, SensorManager.SENSOR_DELAY_GAME);
		} else {
			Toast.makeText(AcelerometroActivity.this, "No accelerometer", Toast.LENGTH_LONG).show();
		}
	}

	protected void onPause() {
		super.onPause();
		mSensorManager.unregisterListener(this);
	}
	
	@Override
	protected void onStop() {
		super.onStop();
	}


	public void onSensorChanged(SensorEvent event) {
			if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
				mGravity = event.values.clone();
			}else if (event.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD) {
				mGeomagnetic = event.values.clone();
			}
			
			if (mGravity != null && mGeomagnetic != null) {
				float R[] = new float[9];
				boolean success = SensorManager.getRotationMatrix(R, null, mGravity, mGeomagnetic);
				if (success) {
					float orientation[] = new float[3];
					SensorManager.getOrientation(R, orientation);
					calculateWheel(orientation);
				}else{
					Toast.makeText(AcelerometroActivity.this, "No success rotation matrix", Toast.LENGTH_LONG).show();
				}
			}else{
				txtview.setText("Init");
			}
	}
	
	public void calculateWheel(float orientation[]){
		try{
			y = (float) Math.toDegrees(orientation[1]);
			z = (float) -Math.toDegrees(orientation[2]);
			
			String str =  String.format("Values  Y = %f   /   Z = %f  \n", y, z);
			float turn = 0;
			float acel = 0;
			if (isRunning){
				
				if ((z > 0) && (Math.abs(y) < 90)){
					turn = (float) (y * (5.5));
					acel = ((posAcel - z)/posAcel) * 350;
				}
				
				if (Math.abs(acel) < 35) acel = 0;
				if (Math.abs(turn) < 25) turn = 0;
				
				String side = "";
				if (turn < 0) side = "Right";
				if (turn > 0) side = "Left";
				
				str += String.format("Running ->  Acel = %f   /   Turn = %f " + side , acel, turn);	
			}
			
			txtview.setText(str);
		} catch (Exception e) {
			txtview.setText(e.getMessage());
		}
	}
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		
		
		final ToggleButton togglebutton = (ToggleButton) findViewById(R.id.toggleButton);
		togglebutton.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				if (togglebutton.isChecked()) {
					if ((z > 0) && (Math.abs(y) < 90)){
						posAcel = z;
					} else {
						posAcel = 50;
					}
					Toast.makeText(AcelerometroActivity.this, "Run", Toast.LENGTH_SHORT).show();
					isRunning = true;
				} else {
					Toast.makeText(AcelerometroActivity.this, "Stop", Toast.LENGTH_SHORT).show();
					isRunning = false;
				}
			}
		});
		
		txtview = (TextView) findViewById(R.id.result);
		
	}

	public void onAccuracyChanged(Sensor sensor, int accuracy) {
		// TODO Auto-generated method stub
	}

}
